#!/bin/bash
# \author lso15
# \ brief test iniziali di wator e visualizer

#attivazioni erronee 
# mancano argomenti
if ./wator ; then
    exit 1
fi

if ./wator -t ; then
    exit 1
fi

if ./wator -v 10 -v 20; then
    exit 1
fi

if ./wator -v 10 -n 20; then
    exit 1
fi

# troppi argomenti
if ./wator __f1 __f2 __f3; then
    exit 1
fi


# file inesistente
if ./wator __file__ ; then
    exit 1
fi

CHECK_FILE=./wator.check
# rimuovo e controllo che il file di check non sia presente
rm -fr ${CHECK_FILE}
if ls ${CHECK_FILE} ; then
    exit 1
fi

# attivazione corretta
./wator planet1.dat &

# attendo 1 secondo e invio SIGUSR1 per forzare il checkpointing
sleep 1
if ! killall -s SIGUSR1 wator ; then
    exit 1
fi

# attendo 1 secondo e controll termino wator controllando 
# se e' ancora attivo e se ha creato il file
sleep 1
if ! killall -w wator && [ -f $CHECK_FILE ]; then
    exit 1
fi


# e' stato terminato correttamente anche il processo visualizer ?
if  killall visualizer ; then
    exit 1
fi

# rimuovo e controllo che il file di check non sia presente
rm -fr ${CHECK_FILE}
if ls ${CHECK_FILE} ; then
    exit 1
fi

# attivazione corretta #2
./wator planet2.dat &

# attendo 1 secondo e invio SIGUSR1 per forzare il checkpointing
sleep 1
if ! killall -s SIGUSR1 wator ; then
    exit 1
fi

# attendo 1 secondo e invio SIGUSR1 per forzare il checkpointing
sleep 1
if ! killall -s SIGUSR1 wator ; then
    exit 1
fi

# attendo 1 secondo e controllo se e' ancora attivo e se ha creato il file
sleep 1
if ! killall -w wator && [ -f $CHECK_FILE] ; then
    exit 1
fi

# e' stato terminato correttamente anche il processo visualizer ?
if  killall visualizer ; then
    exit 1
fi

# rimuovo e controllo che il file di check non sia presente
rm -fr ${CHECK_FILE}
if ls ${CHECK_FILE} ; then
    exit 1
fi

# rimuovo tutti i file di tipo wator_worker_[0-9]*
rm -f "wator_worker_?*"
# rimuovo il dump file del visualizer 
rm -f "visualizer_dump"

# attivazione corretta #3: controllo sul numero di thread worker
./wator planet1.dat -n 4 -f visualizer_dump &

# attendo 1 secondo e invio SIGUSR1 per forzare il checkpointing
sleep 1
if ! killall -s SIGUSR1 wator ; then
    exit 1
fi

# attendo 1 secondo e invio SIGUSR1 per forzare il checkpointing
sleep 1
if ! killall -s SIGUSR1 wator ; then
    exit 1
fi

# attendo 1 secondo e controllo se e' ancora attivo e se ha creato il file
sleep 1
if ! killall -w wator && [ -f $CHECK_FILE ]; then
    exit 1
fi

# e' stato terminato correttamente anche il processo visualizer ?
if  killall visualizer ; then
    exit 1
fi

# controllo che i file wator_worker_? siano stati creati
# controllo che i file wator_worker_? siano stati creati
if  [ ! -f ./wator_worker_0 ] || 
    [ ! -f ./wator_worker_1 ] ||
    [ ! -f ./wator_worker_2 ] ||
    [ ! -f ./wator_worker_3 ]; then
    exit 1
fi

# controllo che il file di dump del visualizer esista
if [ ! -f ./visualizer_dump ] ; then
    exit 1
fi
# .... e che sia identico al file planet1.dat
if ! diff -q ./planet1.dat ./visualizer_dump > /dev/null ; then
    exit 1  # i file sono differenti
fi

exit 0



